﻿#region File Description
//  XAEnima 
//  License: GNU/GPL
//  Developer: Federico Leonardo Motta
//  File description: GUI
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace XAEnima.gui
{
    public class UIcontainer : Item
    {
        DivideByZeroException e = new DivideByZeroException();
        List<UIlabel> labels = new List<UIlabel>();
        List<Vector2> lab_pos = new List<Vector2>();
        Texture2D currentSkin;
        Texture2D activeSkin;
        bool oldstate = false;
        int count = 0;
        static int i = 0;
        Int16 scroll_index = 0;
        int page = 0;
        int current_page=0;
        int elements_number=1;

        public UIcontainer(Game Game)
            : base(Game)
        {
            ID = i;
            Name = "Container" + ID;
            i++;
        }

        public UIcontainer(String n, Vector2 p, string f, Game Game)
            : base(Game, n, p)
        {
            ID = i;
            Skin = Game.Content.Load<Texture2D>(@f);
            currentSkin = Skin;
            Dimension = new Vector2(Skin.Width, Skin.Height);
            i++;
        }

        public UIcontainer(String n, Vector2 p, string f,string s2,Game Game)
            : base(Game,n, p)
        {
            ID = i;
            Skin = Game.Content.Load<Texture2D>(@f);
            activeSkin = Game.Content.Load<Texture2D>(@s2);
            currentSkin = Skin;
            Dimension = new Vector2(Skin.Width,Skin.Height);
            i++;
        }

        public UIcontainer(String n, Vector2 p, string f, string s2,int elements_num, Game Game)
            : base(Game, n, p)
        {
            ID = i;
            Skin = Game.Content.Load<Texture2D>(@f);
            activeSkin = Game.Content.Load<Texture2D>(@s2);
            currentSkin = Skin;
            Dimension = new Vector2(Skin.Width, Skin.Height);
            i++;
            if (elements_num == 0) { 
                throw(e);
            }
            elements_number = elements_num;
        }

        protected override void Dispose(bool disposing)
        {
            Skin.Dispose();
            Game.Services.RemoveService(typeof(UIcontainer));
            base.Dispose(disposing);
        }


        public Vector2 getPageElements(){
            Vector2 estremi = Vector2.Zero;
            estremi.X = current_page * elements_number;
            int rest = labels.Count - (int)estremi.X;
            estremi.Y = (rest < elements_number) ? rest+estremi.X : estremi.X+elements_number;

            return estremi;
        }


        public void AddLabel(UIlabel l,Vector2 pos){
            Vector2 p;
            p.X = Dimension.X * pos.X/10;
            p.Y = Dimension.Y * pos.Y/10;
            p.X = p.X + this.Position.X;
            p.Y = p.Y + this.Position.Y;
            labels.Add(l);
            lab_pos.Add(p);
            count++;
            page = labels.Count / elements_number;
        }

        public List<UIlabel> Labels {
            get { return labels; }
        }

        public Texture2D CurrentSkin
        {
            get { return currentSkin; }
            set { currentSkin = value; }
        }

        public Int16 Index {
            set { scroll_index = value; }
            get { return scroll_index; }
        }

        public int ElementsNumber {
            set { elements_number = value; }
            get { return elements_number; }
        }

        public int Page {
            get { return page; }
        }

        public int CurrentPage {
            set { current_page = value; }
            get { return current_page; }
        }

        public void UpdateContainer(GameTime gameTime)
        {
            if (!oldstate && Select)
            {
                currentSkin = activeSkin;
                oldstate = true;
            }
            else if (oldstate && !Select)
            {
                currentSkin = Skin;
                oldstate = false;
            }

        }
        public UIlabel GetLabel(int id){
            return labels.ElementAt<UIlabel>(id);
        }

        public Vector2 GetLabPos(int id)
        {
            return lab_pos.ElementAt<Vector2>(id);
        }

    }
}
